/*
 * @Author: zoubotao
 * @Date: 2021-04-26 09:39:22
 * @LastEditors: tlp
 * @LastEditTime: 2021-09-02 10:31:15
 * @FilePath: \3DViewer\src\utils\tools.js
 */
/**
 * @description: 001 - 999 三位数显示
 * @param {Number} num
 * @return {String}
 */
export function setNum(num) {
  let len = 3; //显示的长度
  num = (parseInt(num, 10) + 1).toString();
  while (num.length < len) {
    num = '0' + num;
  }
  return num;
}
/**
 * @description: 不足够两位数时，补充前置0
 * @param {Number} num
 * @return {String}
 */
export function addZero(num) {
  return num > 9 ? String(num) : '0' + num;
}

/**
 * 时间格式化
 * @method formatTime
 * @param {String} value
 */
export function formatTime(value) {
  let time = new Date(value.slice(0, 19) + 'Z');
  return `${time.getFullYear()}-${addZero(time.getMonth() + 1)}-${addZero(
    time.getDate()
  )} ${addZero(time.getHours())}:${addZero(time.getMinutes())}:${addZero(time.getSeconds())}`;
}

/**
 * @description: 生成DataURL格式的渐变色，用于echarts中的backgroundColor需要使用渐变色的情形
 * @param {Number} width
 * @param {Number} height
 * @param {Array} colorStops
 * @return {String}
 */
export function createLinearGradientDataUrl(width, height, colorStops) {
  const canvas = document.createElement('canvas');
  canvas.style.width = `${width}px`;
  canvas.style.height = `${height}px`;
  canvas.width = width;
  canvas.height = height;
  const ctx = canvas.getContext('2d'),
    gradient = ctx.createLinearGradient(0, 0, width, height);
  colorStops.forEach((item) => gradient.addColorStop(...item));
  ctx.fillStyle = gradient;
  ctx.fillRect(0, 0, width, height);
  return canvas.toDataURL();
}

/**
 * @description: 千分位分割
 * @param {Number} 数值
 * @return {String}
 */
export function valueFilter(val) {
  return (val || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
}

/**
 * @description: Date对象转为yyyy-MM-dd HH:mm:ss格式
 * @param {Date} dateObj 日期对象
 * @param {Boolean} onlyDate 是否返回仅仅日期
 * @return {String} 返回yyyy-MM-dd HH:mm:ss格式字符串
 */
export function dateFormat(dateObj, onlyDate = false) {
  const year = dateObj.getFullYear(),
    month = addZero(dateObj.getMonth() + 1),
    date = addZero(dateObj.getDate());
  if (onlyDate) return `${year}-${month}-${date}`;
  const hour = addZero(dateObj.getHours()),
    minute = addZero(dateObj.getMinutes()),
    second = addZero(dateObj.getSeconds());
  return `${year}-${month}-${date} ${hour}:${minute}:${second}`;
}
/**
 * @description: Date对象转为yyyyMMd格式
 * @param {Date} dateObj 日期对象
 * @param {Boolean} onlyDate 是否返回仅仅日期
 * @return {String} 返回yyyyMMdd 格式字符串
 */
export function dateFormatNO(data) {
  let dateObj = new Date(data);
  const year = dateObj.getFullYear(),
    month = addZero(dateObj.getMonth() + 1),
    date = addZero(dateObj.getDate());
  return `${year}${month}${date}`;
}
/**
 * @description: 获取当前月的1号
 * @return {String} 返回当前月的1号 yyyy-MM-dd HH:mm:ss
 */
export function getDate() {
  const myDate = new Date();
  return `${myDate.getFullYear()}-${addZero(myDate.getMonth() + 1)}-01 00:00:00`;
}

/**
 * @description 生成某个区段的随机数
 * @return {Number} 生成的随机数
 */
export function getRandomNum(min, max) {
  return min + Math.round(Math.random() * (max - min));
}
/**
 * @description: 获得某月的天数
 * @return {String} 返回某月的天数
 */
export function getMonthDays(myMonth) {
  var now = new Date(); //当前日期
  var nowYear = now.getYear(); //当前年
  nowYear += nowYear < 2000 ? 1900 : 0; //
  var monthStartDate = new Date(nowYear, myMonth, 1);
  var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
  return days;
}
/**
 * @description: 获得去年同期的时间
 * @return {String} 返回去年同期时间
 */
export function getPreYearDate(time, type, year) {
  var now = new Date(time); //当前日期
  var nowMin = addZero(now.getMinutes()); //当前时间
  var nowS = addZero(now.getSeconds()); //当前时间
  var nowHours = addZero(now.getHours()); //当前时间
  var nowDay = addZero(now.getDate()); //当前日
  var nowMonth = addZero(now.getMonth() + 1); //当前月
  var nowYear = now.getFullYear() - 1; //去年
  nowYear = year ? now.getFullYear() + 1 : nowYear;
  var days = !type
    ? `${nowYear}-${nowMonth}-${nowDay} ${nowHours}:${nowMin}:${nowS}`
    : `${nowYear}-${nowMonth}-${nowDay} 00:00:00`;
  return days;
}
/**
 *获取日期区间的每一个日期，返回日期数组
 *@method getEveryDayDateByBetweenDate
 *@start_date {date} 开始日期
 *@end_date   {date} 结束日期
 *@return {arr} 返回日期数组
 */
export function getEveryDayDateByBetweenDate(begin, end) {
  // console.log(begin, end);
  var arr = [];
  var ab = begin.split('-');
  var ae = end.split('-');
  var db = new Date();
  db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
  var de = new Date();
  de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
  var unixDb = db.getTime() - 24 * 60 * 60 * 1000;
  var unixDe = de.getTime() - 24 * 60 * 60 * 1000;
  for (var k = unixDb; k <= unixDe; ) {
    //console.log((new Date(parseInt(k))).format());
    k = k + 24 * 60 * 60 * 1000;
    arr.push(dateFormat(new Date(parseInt(k)), true));
  }
  return arr;
}
/**
 *
 * @description: 将时间"20180101"转为"2018-01-01"
 */
export function changeDate(value) {
  let str = value.replace(/^(.{4})(.{2})(.{2})$/, '$1-$2-$3');
  return str;
}
/**
 * 获取本周、上周、下周
 * @param {Number} type 类型 - 上周：0 - 下周：1 - 本周：默认
 * @return {String} 返回开始日期 -- 结束日期
 */
export function getWeekTime(type) {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth();
  var day = now.getDate(); //当前日
  var dayOfWeek = now.getDay(); //返回星期几的某一天;
  let n1 = 0,
    n2 = 0;
  switch (type) {
    case 0:
      n1 = 7;
      n2 = 1;
      break;
    case 1:
      n1 = -7;
      n2 = -13;
      break;
    default:
      n1 = 0;
      n2 = -6;
      break;
  }
  var startTime = new Date(year, month, day - (dayOfWeek == 0 ? n1 + 6 : n1 + (dayOfWeek - 1)));
  var endTime = new Date(year, month, day - (dayOfWeek == 0 ? n2 + 6 : n2 + (dayOfWeek - 1)));
  let returnData = { startTime: dateFormat(startTime, true), endTime: dateFormat(endTime, true) };
  // console.log(!type && type != 0 ? '本' : type == 0 ? '上' : '下', '周', returnData);
  return returnData;
}
/**
 * 获取本月、上月、下月
 * @param {Number} type 类型 - 上月：0  - 下月：1 - 本月：默认
 * @return {String} 返回开始日期 -- 结束日期
 */
export function getMothTime(type) {
  var now = new Date();
  var year = now.getFullYear();
  let n = null;
  switch (type) {
    case 0:
      n = -1;
      break;
    case 1:
      n = 1;
      break;
    default:
      n = 0;
      break;
  }
  var newMonth = now.getMonth() + n;
  var newDay = new Date(year, newMonth, 0).getDate();
  var startTime = new Date(year, newMonth, 1);
  var endTime = new Date(year, newMonth, newDay);
  let returnData = { startTime: dateFormat(startTime, true), endTime: dateFormat(endTime, true) };
  // console.log(!type && type != 0 ? '本' : type == 0 ? '上' : '下', '月', returnData);
  return returnData;
}
/**
 * 获取本年、上年、下年
 * @param {Number} type 类型 - 上年：0  - 下年：1 - 本年：默认
 * @return {String} 返回开始日期 -- 结束日期
 */
export function getYearTime(type) {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth();
  var day = now.getDate(); //当前日
  var newYear = null;
  switch (type) {
    case 0:
      newYear = now.getFullYear() - 1;
      break;
    case 1:
      newYear = now.getFullYear() + 1;
      break;
    default:
      newYear = year;
      break;
  }
  var startTime = new Date(newYear, month, day);
  var endTime = new Date(newYear, month, day);
  let returnData = { startTime: dateFormat(startTime, true), endTime: dateFormat(endTime, true) };
  // console.log(!type && type != 0 ? '本' : type == 0 ? '上' : '下', '年', returnData);
  return returnData;
}
/**
 *
 * @description: 预测、实时周、月、年接口对接
 *  @param {Number} type 接口类型 - 实时：0 - 预测：1
 * @param {Number} btnType 按钮对应时间 - 周：0 - 月：1 - 年：2
 * @param {Number} timeType 上周/月/年：0  - 本周/月/年：1 - 下年周/月/年：2
 * * @return {String} 返回某月的天数
 */
export function getLocationPredictDate(type, btnType, timeType) {
  let param = {};
  //实时apiCode -- 周/月/年
  const location = [
    'passflow_location_week_history',
    'passflow_location_month_history',
    'passflow_location_year_history'
  ];
  //预测apiCode -- 周/月/年
  const predict = [
    'passflow_predict_location_week',
    'passflow_predict_location_month',
    'passflow_predict_location_month_2'
  ];
  const timeStr = ['dateStr', 'monthStr', 'yearStr']; //周/月/年
  let myDate = new Date();
  if (timeType == 0) {
    //上周/月/年
    const last =
      btnType == 0
        ? getWeekTime(0).startTime
        : btnType == 1
        ? getMothTime(0).startTime
        : getYearTime(0).startTime;
    myDate = new Date(last);
  } else if (timeType == 1) {
    //本周/月/年
    const current =
      btnType == 0
        ? getWeekTime().startTime
        : btnType == 1
        ? getMothTime().startTime
        : getYearTime().startTime;
    myDate = new Date(current);
  } else if (timeType == 2) {
    //下周/月/年
    const next =
      btnType == 0
        ? getWeekTime(1).startTime
        : btnType == 1
        ? getMothTime(1).startTime
        : getYearTime(1).startTime;
    myDate = new Date(next);
  }
  const year = myDate.getFullYear();
  const month = addZero(myDate.getMonth() + 1);
  const date = addZero(myDate.getDate());
  param = {
    apiCode: type == 0 ? location[btnType] : predict[btnType]
  };
  param[timeStr[btnType]] = btnType == 0 ? year + month + date : btnType == 1 ? year + month : year;
  if (btnType == 0) {
    param = {
      ...param,
      yearStr: year
    };
  }
  return param;
}
/**
 * @description: 获得一天内间隔一小时数组
 */
export function dayHoursTime() {
  const date = [
    '00:00',
    '01:00',
    '02:00',
    '03:00',
    '04:00',
    '05:00',
    '06:00',
    '07:00',
    '08:00',
    '09:00',
    '10:00',
    '11:00',
    '12:00',
    '13:00',
    '14:00',
    '15:00',
    '16:00',
    '17:00',
    '18:00',
    '19:00',
    '20:00',
    '21:00',
    '22:00',
    '23:00'
  ];
  return date;
}
/**
 * @description: 获得7天前的日期
 */
export function getDay(day) {
  var today = new Date();
  var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds); //注意，这行是关键代码
  var tYear = today.getFullYear();
  var tMonth = today.getMonth();
  var tDate = today.getDate();
  tMonth = addZero(tMonth + 1);
  tDate = addZero(tDate);
  return tYear + '-' + tMonth + '-' + tDate;
}
